using UnityEngine;
using System.Collections;

public class Listener : MonoBehaviour 
{
	public Synch synch;
	
	void Update ()
	{
		handleMouse();
		handleKeyboard();
	}
	
	private MouseMes mouMes;
	private bool mouseDown = false;
	private Vector3 mouseXY = new Vector3(0.0f,0.0f);
	private Vector3 mousePos = new Vector3(0.0f, 0.0f);
	
	private void handleMouse()
	{
		//left click
		if(Input.GetMouseButtonDown(0))
		{
			mouseDown = true;
			mouseXY = Input.mousePosition;
			mousePos = Camera.main.ScreenToWorldPoint(mouseXY);
		}
		if(mouseDown && Input.GetMouseButtonUp(0))
		{
			mouseDown = false;
			if(mousePos == Camera.main.ScreenToWorldPoint(Input.mousePosition))
			{
				//send mouse click message
				mouMes = new MouseMes(MouseMessageType.LEFT_CLICK, mousePos, synch.model.playerNum);
				mouMes.pack();
				synch.Send(mouMes);
			}
			else
			{
				//send area message
				mouMes = new MouseMes(MouseMessageType.AREA_SELECT, Camera.main.ScreenToWorldPoint(mouseXY),
					Camera.main.ScreenToWorldPoint(Input.mousePosition) - mousePos, synch.model.playerNum);
				mouMes.pack();
				synch.Send(mouMes);
			}
		}
		//right click
		if(Input.GetMouseButtonDown(1))
		{
			//send right click message
			mouMes = new MouseMes(MouseMessageType.RIGHT_CLICK,Camera.main.ScreenToWorldPoint(Input.mousePosition), synch.model.playerNum);
			mouMes.pack();
			synch.Send(mouMes);
		}
		if(Input.GetMouseButtonDown(2))
		{
			//send middle click message
			mouMes = new MouseMes(MouseMessageType.MIDDLE_CLICK, Camera.main.ScreenToWorldPoint(Input.mousePosition), synch.model.playerNum);
			mouMes.pack();
			synch.Send(mouMes);
		}
	}
	
	private KBMes kbMes;
	
	private void handleKeyboard()
	{
		//keyboard
			//keydown
		if(Input.GetKeyDown(KeyCode.Alpha0))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.ZERO);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyDown(KeyCode.Alpha1))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.ONE);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyDown(KeyCode.Alpha2))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.TWO);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyDown(KeyCode.Alpha3))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.THREE);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyDown(KeyCode.Alpha4))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.FOUR);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyDown(KeyCode.Alpha5))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.FIVE);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyDown(KeyCode.Alpha6))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.SIX);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyDown(KeyCode.Alpha7))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.SEVEN);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyDown(KeyCode.Alpha8))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.EIGHT);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyDown(KeyCode.Alpha9))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.NINE);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.SHIFT);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_DOWN, KEY.CONTROL);
			kbMes.pack();
			synch.Send(kbMes);
		}
		
			//keyup
		if(Input.GetKeyUp(KeyCode.Alpha0))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.ZERO);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyUp(KeyCode.Alpha1))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.ONE);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyUp(KeyCode.Alpha2))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.TWO);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyUp(KeyCode.Alpha3))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.THREE);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyUp(KeyCode.Alpha4))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.FOUR);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyUp(KeyCode.Alpha5))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.FIVE);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyUp(KeyCode.Alpha6))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.SIX);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyUp(KeyCode.Alpha7))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.SEVEN);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyUp(KeyCode.Alpha8))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.EIGHT);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyUp(KeyCode.Alpha9))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.NINE);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.SHIFT);
			kbMes.pack();
			synch.Send(kbMes);
		}
		if(Input.GetKeyUp(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl))
		{
			kbMes = new KBMes(KeyboardMessageType.KEY_UP, KEY.CONTROL);
			kbMes.pack();
			synch.Send(kbMes);
		}
	}
}
